package com.rui.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;

/**
 * Created by lee on 17/04/2017.
 */
@Component
@Order(1)
public class LogAspect {

    Logger logger = LoggerFactory.getLogger(LogAspect.class);

    public void beforeExecute(JoinPoint joinPoint) throws Throwable {
        logger.info(">>>>>>>>>>> beforeExecute "+joinPoint.getSignature().getName());
    }

    public void afterExecute(JoinPoint joinPoint) throws Throwable {
        logger.info(">>>>>>>>>>> afterExecute "+joinPoint.getSignature().getName());
    }

    public void afterThrowing(JoinPoint joinPoint) throws Throwable {
        logger.error(">>>>>>>>>>> afterThrowing "+joinPoint.getSignature().getName());
    }

    public void afterReturning(JoinPoint joinPoint) throws Throwable {
        logger.info(">>>>>>>>>>> afterReturning "+joinPoint.getSignature().getName());
    }

    public Object around(ProceedingJoinPoint point) throws Throwable {
        logger.info("params:"+Arrays.toString(point.getArgs()));
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        Log log = new Log();
//        String url = request.getRequestURL().toString(); // URL 路径
//        log.setUrl(url);
//        String userAgent  = request.getHeader("User-Agent"); // 浏览器
//        log.setUserAgent(userAgent);
//        // ip 地址获取
//        String ip = request.getHeader("x-forwarded-for");
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("WL-Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getRemoteAddr();
//        }
//        log.setIp(ip);
//        log.setArgs(Arrays.toString(point.getArgs()));
//        String className = point.getThis().getClass().getName(); // 类名
//        if (className.indexOf("$$EnhancerBySpringCGLIB$$") > -1) { // 如果是CGLIB动态生成的类,删除标示
//            try {
//                className = className.substring(0, className.indexOf("$$"));
//            } catch (Exception ex) {
//                ex.printStackTrace();
//            }
//        }
//        String methodName = className + "." + point.getSignature().getName();//方法名
//        log.setMethodName(methodName);
//        //计算方法执行时间
//        StopWatch watch = new StopWatch();
//        watch.start();
//        Object object = point.proceed();
//        watch.stop();
//        log.setTotalTime(watch.getTotalTimeMillis());
//        logger.info("请求URL："+url+" IP 地址："+ip+" User-Agent："+userAgent + " url响应时间（毫秒）"+ watch.getTotalTimeMillis());
//        return object;
        return null;
    }
}